package com.fms.gui.existingSheets;

import java.awt.Component;
import java.util.HashMap;
import java.util.Map;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

import com.fms.gui.existingSheets.buttons.IButtonBuilder;
import com.fms.model.domain.FlowSheet;

public class ButtonRenderer extends AbstractCellEditor implements TableCellRenderer, TableCellEditor
{
	/**
	 * 
	 */
	private static final long	serialVersionUID	= -2069021981254975526L;
	private Map<String, JButton> buttons = new HashMap<String, JButton>();
	private IButtonBuilder	buttonMaker;
	
	public ButtonRenderer(IButtonBuilder _buttonMaker)
	{
		this.buttonMaker = _buttonMaker;
	}
	
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean arg2, boolean arg3, int arg4, int arg5)
	{
		return getButton(value);
	}

	@Override
	public Object getCellEditorValue()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected, int row, int column)
	{
		return getButton(value);
	}

	private Component getButton(Object value)
	{
		if(value instanceof FlowSheet)
		{
			FlowSheet f = ((FlowSheet)value);
			JButton button = buttons.get(f.getFileName());
			if(button == null)
			{
				button = buttonMaker.makeButton(f);
				buttons.put(f.getFileName(), button);
			}
			return button;
		}
		return null;
	}



}
